package com.cn.wanxi.servlet.back.user;

import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.service.IUserService;
import com.cn.wanxi.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

@WebServlet("/back/user/add")
public class UserAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("../html/back/useradd.html").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        IUserService userService = new UserServiceImpl();
        UserModel userModel = new UserModel();
        userModel.setUsername(req.getParameter("username"));
        userModel.setPassword(req.getParameter("password"));
        userModel.setIntro(req.getParameter("intro"));
        userModel.setBirth(req.getParameter("birth"));
        userModel.setPhone(req.getParameter("phone"));
        userModel.setSex(req.getParameter("sex"));
        userModel.setHobby(req.getParameter("hobby"));
        userModel.setCreateTime(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date().getTime()));
        int result = userService.addUser(userModel);
        resp.getWriter().print(result);
    }
}
